package GUI;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;

import logic.Server;
import java.awt.Font;
import java.net.BindException;

public class StartPanel extends JPanel {
	/**
	 * 
	 */

	private static final long serialVersionUID = 1L;
	private JTextField n_version;
	private JTextField n_port;


	public StartPanel(final Server server) {
		setLayout(null);

		JLabel lblWantsToUse = new JLabel("Wants to use improvements?");
		lblWantsToUse.setFont(new Font("Tahoma", Font.BOLD, 18));
		lblWantsToUse.setBounds(139, 46, 298, 14);
		add(lblWantsToUse);

		final JRadioButton rdbtnYes = new JRadioButton("Yes");
		rdbtnYes.setFont(new Font("Tahoma", Font.PLAIN, 16));
		rdbtnYes.setBounds(140, 67, 109, 23);
		add(rdbtnYes);

		final JRadioButton rdbtnNo = new JRadioButton("No");
		rdbtnNo.setFont(new Font("Tahoma", Font.PLAIN, 16));
		rdbtnNo.setSelected(true);
		rdbtnNo.setBounds(370, 67, 109, 23);


		add(rdbtnNo);

		ButtonGroup group = new ButtonGroup();
		group.add(rdbtnNo);
		group.add(rdbtnYes);

		JLabel lblNumberOfVersion = new JLabel("Number of Version:");
		lblNumberOfVersion.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblNumberOfVersion.setBounds(273, 126, 141, 14);
		add(lblNumberOfVersion);

		n_version = new JTextField();
		n_version.setFont(new Font("Tahoma", Font.PLAIN, 16));
		n_version.setEnabled(false);
		n_version.setText("2.0");
		n_version.setBounds(422, 123, 86, 20);
		add(n_version);
		n_version.setColumns(10);

		JLabel lblNumberOfPort = new JLabel("Number of Port:");
		lblNumberOfPort.setFont(new Font("Tahoma", Font.PLAIN, 16));
		lblNumberOfPort.setBounds(10, 126, 141, 14);
		add(lblNumberOfPort);

		n_port = new JTextField();
		n_port.setFont(new Font("Tahoma", Font.PLAIN, 16));
		n_port.setEnabled(false);
		n_port.setText("1234");
		n_port.setColumns(10);
		n_port.setBounds(163, 123, 86, 20);
		add(n_port);

		rdbtnNo.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent arg0) {

				
					n_port.setEnabled(false);
					n_version.setEnabled(false);
				
			}
		});

		rdbtnYes.addMouseListener(new MouseAdapter() {
			@Override
			public void mousePressed(MouseEvent arg0) {

				
					n_port.setEnabled(true);
					n_version.setEnabled(true);
				
			}
		});
		JButton btnOk = new JButton("Ok");
		btnOk.setFont(new Font("Tahoma", Font.PLAIN, 16));
		btnOk.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				try
				{
				if(rdbtnYes.isSelected())
				{
					
					Server.enhancement=true;
					Server.port=Integer.parseInt(n_port.getText());
					Server.VERSION=n_version.getText();
				}
			
					server.run();
				
				GUI.frame.setContentPane(new MainPanel(server));

				((JPanel)GUI.frame.getContentPane()).revalidate();
				GUI.frame.repaint();
				}catch(Exception b)
				{
					b.printStackTrace();
					if(b instanceof BindException)
						JOptionPane.showMessageDialog(GUI.frame,"Port already in use", "Error",JOptionPane.ERROR_MESSAGE);
					else
						JOptionPane.showMessageDialog(GUI.frame,"Incorrect parameters", "Error",JOptionPane.ERROR_MESSAGE);
				}
			}
		});
		btnOk.setBounds(174, 194, 89, 23);
		add(btnOk);

		JButton btnExit = new JButton("Exit");
		btnExit.setFont(new Font("Tahoma", Font.PLAIN, 16));
		btnExit.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				System.exit(0);
			}
		});
		btnExit.setBounds(289, 194, 89, 23);
		add(btnExit);

	}




}
